package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.BWeixinurl;

public class BWeixinurlDao extends SuperDao {

	public Object getWeixinurlById(BWeixinurl bWeixinurl) {
		String hql = "from BWeixinurl where id = ?";
		Object[] values = new Object[] { bWeixinurl.getId() };
		List<Object> list = super.getObject(hql, values);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<Object> getWeixinurl() {
		String hql = "from BWeixinurl";
		return super.find(hql);
	}

	/**
	 * 查询企业的url
	 */
	public List<Object> getWeixinurlByEaId(Long eaId) {
		String hql = "from BWeixinurl where eaId = ?";
		return super.getObject(hql, new Object[] { eaId });
	}
	
	/**
	 * 菜单查询企业url
	 */
	public List<Object> getWeixinMenuUrlByEaId(Long eaId) {
		String hql = "from BWeixinurl where eaId = ? or eaId = 0";
		return super.getObject(hql, new Object[] { eaId });
	}

	/**
	 * 保存新链接
	 */
	public void addWeixinUrl(BWeixinurl bWeixinurl) {
		super.save(bWeixinurl);
	}

	/**
	 * 删除链接
	 */
	public void delWeixinUrl(BWeixinurl bWeixinurl) {
		super.delete(bWeixinurl);
	}

	/**
	 * 修改链接
	 */
	public void modifyWeixinUrl(BWeixinurl bWeixinurl) {
		String separate = " set ";
		StringBuilder sb = new StringBuilder();
		sb.append("update b_weinxin_url");

		String url = bWeixinurl.getUrl();
		if (StringUtils.isNotBlank(url)) {
			sb.append(separate);
			sb.append("url = '");
			sb.append(url);
			sb.append("'");
			separate = ", ";
		}
		
		String name = bWeixinurl.getName();
		if (StringUtils.isNotBlank(name)) {
			sb.append(separate);
			sb.append("name = '");
			sb.append(name);
			sb.append("'");
			separate = ", ";
		}

		sb.append(separate);
		sb.append("description = '");
		sb.append(bWeixinurl.getDescription());
		sb.append("'");

		sb.append(" where id = ");
		sb.append(bWeixinurl.getId());

		super.executeUpdate(sb.toString());
	}

}
